import requests
import re
import json

try:
    with open('仓库地址.txt', 'r', encoding='utf-8') as file:
        repo_urls = file.read().splitlines()

    repo_info_list = []

    for url in repo_urls:
        response = requests.get(url)
        repo_name = url.split('/')[-1]
        repo_info = {
            "仓库名称": repo_name
        }
        if response.status_code == 200:
            # 查找最新提交时间
            time_match = re.search(r"<span class='timeago' datetime='(.*?)' title='(.*?)'></span>", response.text)
            if time_match:
                repo_info["最新提交时间"] = time_match.group(1)
            else:
                repo_info["最新提交时间"] = "未找到匹配的提交时间"

            # 查找提交次数
            count_match = re.search(r"\n(.*?)次提交\n", response.text)
            if count_match:
                repo_info["提交次数"] = count_match.group(1)
            else:
                repo_info["提交次数"] = "未找到提交次数信息"
        else:
            repo_info["请求状态"] = f"状态码:{response.status_code}"

        repo_info_list.append(repo_info)

    with open('提交时间.json', 'w', encoding='utf-8') as output_file:
        json.dump(repo_info_list, output_file, ensure_ascii=False, indent=4)
    print("仓库信息已成功保存到 提交时间.json")
except FileNotFoundError:
    print("错误：未找到 仓库地址.txt 文件，请确保该文件存在。")
except Exception as e:
    print(f"发生未知错误：{e}")


